package com.zhuozhengsoft.pageoffice6springboot2back.controller;

import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice6springboot2back.po.BingFaDoc;
import com.zhuozhengsoft.pageoffice6springboot2back.util.GetDirPathUtil;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/BingFa")
public class BingFaController {
    //获取doc目录的磁盘路径
    private final String dir = GetDirPathUtil.getDirPath() + "/static/doc/";
    @RequestMapping(value = "/index")
    public List<BingFaDoc> index(String userName) throws ClassNotFoundException, SQLException, FileNotFoundException {
        Class.forName("org.sqlite.JDBC");
        String strUrl = "jdbc:sqlite:" + ResourceUtils.getURL("classpath:").getPath() + "static/demodata/BingFa.db";
        Connection conn = DriverManager.getConnection(strUrl);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("select * from doc order by id desc");
        List<BingFaDoc> docBingFaList = new ArrayList<>();
        while (rs.next()) {
            BingFaDoc bingFaDoc = new BingFaDoc();
            bingFaDoc.setId(rs.getInt("Id"));
            bingFaDoc.setFileName(rs.getString("FileName"));
            bingFaDoc.setSubject(rs.getString("Subject"));
            bingFaDoc.setEditor(rs.getString("Editor"));
            docBingFaList.add(bingFaDoc);
        }
        rs.close();
        stmt.close();
        conn.close();
        return docBingFaList;
    }

    @RequestMapping(value = "/Word1")
    public String showWord(HttpServletRequest request, String subject, String userName, String fileName) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        poCtrl.setCaption("当前用户：" + userName + " | 模式：修订编辑 | 文档名称：" + subject);
        poCtrl.webOpen("/doc/BingFa/" + fileName, OpenModeType.docRevisionOnly, userName);
        return poCtrl.getHtml();
    }

    @RequestMapping(value = "/Word2")
    public String showWord2(HttpServletRequest request, String subject, String userName, String fileName) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        poCtrl.setCaption("当前用户：" + userName + " | 模式：只读打开 | 文档名称：" + subject);
        poCtrl.webOpen("/doc/BingFa/" + fileName, OpenModeType.docReadOnly, userName);
        return poCtrl.getHtml();
    }
    @RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response, String fileName) {
        FileSaver fs = new FileSaver(request, response);
        fs.saveToFile(dir + "BingFa/" + fileName);
        fs.close();
    }

    @PutMapping("/updateEditorById")
    public Integer updateEditorById(int id,String userName) throws Exception {
        Class.forName("org.sqlite.JDBC");
        String strUrl = "jdbc:sqlite:" + ResourceUtils.getURL("classpath:").getPath() + "static/demodata/BingFa.db";
        Connection conn = DriverManager.getConnection(strUrl);
        Statement stmt = conn.createStatement();
        int rs = stmt.executeUpdate("Update doc set Editor='"+ userName +"' where id="+id);;
        if (rs>0){
            System.out.println("更新成功");
        }
        stmt.close();
        conn.close();
        return  rs;
    }

    @RequestMapping("/selectDocById")
    public BingFaDoc selectDocById(int id) throws Exception {
        Class.forName("org.sqlite.JDBC");
        String strUrl = "jdbc:sqlite:" + ResourceUtils.getURL("classpath:").getPath() + "static/demodata/BingFa.db";
        Connection conn = DriverManager.getConnection(strUrl);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("select * from doc where id="+id);
        BingFaDoc bingFaDoc = new BingFaDoc();
        while (rs.next()) {
            bingFaDoc.setId(rs.getInt("Id"));
            bingFaDoc.setFileName(rs.getString("FileName"));
            bingFaDoc.setSubject(rs.getString("Subject"));
            bingFaDoc.setEditor(rs.getString("Editor"));
        }
        rs.close();
        stmt.close();
        conn.close();
        return bingFaDoc;
    }

}
